package com.ecar.common.dto;

import com.ecar.common.util.JSONSerialization;
import org.codehaus.jackson.annotate.JsonIgnore;

import java.util.HashMap;
import java.util.List;

/**
 * 查询结果分页信息对象
 * @author admin
 */
public class Pager extends DataBean implements JSONSerialization {

	public static final Integer MAX_PAGE_SIZE = 500;

	/* 当前页 */
	private Integer pageNumber = 1;

	/* 每页记录数 */
	private Integer pageSize = 10;

	/* 总记录数 */
	private Integer totalCount = 0;

	/* 总页数 */
	private Integer pageCount = 0;

	/* 查询排序字段 */
	private String orderBy = "";

	/* 查询字段 (对应bean属性):值 支持多个条件 */
	private HashMap queryMap;

	/* 查询字段(对应bean属性)的名字 */
	private String queryProperty;

	/* 查询字段(对应bean属性)的值列表 */
	private List queryList;

	/* 排序类型 */
	private OrderType orderType = OrderType.desc;

	public Pager() {
		super();
	}

	public Pager(HashMap queryMap) {
		super();
		this.queryMap = queryMap;
	}

	public Integer getPageNumber() {
		return this.pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		if (pageNumber < 1) {
			pageNumber = 1;
		}
		this.pageNumber = pageNumber;
	}

	public Integer getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(Integer pageSize) {
		if (pageSize < 1)
			pageSize = 1;
		else if (pageSize > MAX_PAGE_SIZE) {
			pageSize = MAX_PAGE_SIZE;
		}
		this.pageSize = pageSize;
	}

	public Integer getTotalCount() {
		return this.totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public Integer getPageCount() {
		this.pageCount = this.totalCount / this.pageSize;
		if (this.totalCount % this.pageSize > 0) {
			Pager tmp41_40 = this;
			tmp41_40.pageCount = tmp41_40.pageCount + 1;
		}
		return this.pageCount;
	}

	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}

	@JsonIgnore
	public String getOrderBy() {
		return this.orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	@JsonIgnore
	public OrderType getOrderType() {
		return this.orderType;
	}

	public void setOrderType(OrderType orderType) {
		this.orderType = orderType;
	}

	public enum OrderType {
		asc, desc
	}

	@JsonIgnore
	public HashMap getQueryMap() {
		return queryMap;
	}

	public void setQueryMap(HashMap queryMap) {
		this.queryMap = queryMap;
	}

	@JsonIgnore
	public List getQueryList() {
		return queryList;
	}

	public void setQueryList(List queryList) {
		this.queryList = queryList;
	}

	@JsonIgnore
	public String getQueryProperty() {
		return queryProperty;
	}

	public void setQueryProperty(String queryProperty) {
		this.queryProperty = queryProperty;
	}

	@JsonIgnore
	public String getValueString(String key) {
		if (queryMap == null) return null;
		try {
			return (String) queryMap.get(key);
		} catch (Exception e) {
			return null;
		}
	}

	@JsonIgnore
	public int getValueInt(String key) {
		if (queryMap == null || !queryMap.containsKey(key)) return -1;
		try {
			return (Integer) queryMap.get(key);
		} catch (Exception e) {
			return -1;
		}
	}

	@JsonIgnore
	public Boolean getValueBool(String key) {
		if (queryMap == null) return null;
		try {
			return (Boolean) queryMap.get(key);
		} catch (Exception e) {
			return null;
		}
	}

	@JsonIgnore
	public long getValueLong(String key) {
		if (queryMap == null) return 0L;
		try {
			return (Long) queryMap.get(key);
		} catch (Exception e) {
			return 0L;
		}
	}

	@JsonIgnore
	public boolean hasQueryMap() {
		return queryMap != null && !queryMap.isEmpty();
	}

	@JsonIgnore
	public boolean contains(String key) {
		return queryMap != null && queryMap.containsKey(key);
	}
}
